package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServicelmpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    public List<ShoppingCart> getCar() {
        List<ShoppingCart> shoppingCarts = new ArrayList<>();
        shoppingCarts = shoppingCartMapper.getCar(BaseContext.getCurrentId());
        return shoppingCarts;
    }


    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //判断是否已经在购微车
        Long userId = BaseContext.getCurrentId();//获取当前用户
        log.info("id",userId);
        //使用动态SQL查询是否有数据,返回该购物车对象
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart = shoppingCartMapper.HavingInCar(shoppingCartDTO, userId);

        //存在数据
        if (shoppingCart != null) {
            //采用更新操作
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.UpdateNumber(shoppingCart);
        } else {
            //采用插入操作
            shoppingCart = new ShoppingCart(); // 确保创建新的 ShoppingCart 对象
            BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
            shoppingCart.setUserId(userId);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            //判断传入商品类型,完善商品信息
            if (shoppingCart.getSetmealId() != null) {
                //传入的是套餐
                Setmeal setmeal = shoppingCartMapper.getSetmeal(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            } else {
                //传入的是菜品
                Dish dish = shoppingCartMapper.getDish(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }
            shoppingCartMapper.add(shoppingCart);
        }
    }


    /**
     * 删除购物车中一个商品
     *
     * @param shoppingCartDTO
     * @return
     */
    public void SubCar(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        Long userId = BaseContext.getCurrentId();
        shoppingCart = shoppingCartMapper.HavingInCar(shoppingCartDTO, userId);
        //判断数量是否唯为1
        if (shoppingCart.getNumber() == 1) {
            //等于1执行清除
            shoppingCartMapper.SubCar(shoppingCart);
        } else {
            //数量减一
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
             shoppingCartMapper.jianyi(shoppingCart);
        }
    }


    public void clean() {
        Long id = BaseContext.getCurrentId();
        BaseContext.removeCurrentId();
        shoppingCartMapper.clean(id);
    }
}
